package com.yuzhitong.shapi.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;

import com.kwad.sdk.api.KsAdSDK;
import com.kwad.sdk.api.KsHotspotPage;
import com.kwad.sdk.api.KsLoadManager;
import com.kwad.sdk.api.KsScene;
import com.yuzhitong.shapi.base.BaseLayoutFragment;
import com.yuzhitong.shapi.base.Contents;
import com.yuzhitong.shapi.util.LoggerUtil;

/**
 * Created by Brsya
 * CreateDate: 2022/5/1  1:20
 * Description:
 * 热榜
 */
public class HotFragment extends BaseLayoutFragment {


    public static HotFragment newInstance() {

        Bundle args = new Bundle();

        HotFragment fragment = new HotFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected void initData() {
        KsScene scene = new KsScene.Builder(Contents.KS_SDK_HOT_ID)
                .width(getScreenWidth(getActivity()))
                .build();
        KsAdSDK.getLoadManager().loadHotspotPage(scene, new KsLoadManager.KsHotSpotPageListener() {
            @Override
            public void onError(int code, String msg) {
                if (getActivity() == null || getActivity().isFinishing()) {
                    return;
                }
                //                        Toast.makeText(getActivity(), "加载热榜组件失败", Toast.LENGTH_SHORT).show();
                LoggerUtil.e("加载热榜组件失败" + code + " ---- " + msg);
            }

            @Override
            public void onKsHotspotPageLoad(KsHotspotPage ksHotspotPage) {
                if (getActivity() == null || getActivity().isFinishing()) {
                    return;
                }
                setContentView(ksHotspotPage.getHotspotEntryView(getActivity()));
            }
        });
    }

    @Override
    protected void initView() {

    }


    /**
     * 优先使用 getDisplayWidth/getDisplayHeight
     */
    public static int getScreenWidth(Activity activity) {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels;
    }
}
